package com.example.apigateway.swagger;


import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/** @author yang */
@Component
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory {

  @Override
  public GatewayFilter apply(Object config) {
    return (exchange, chain) -> {
      ServerHttpRequest request = exchange.getRequest();
      String path = request.getURI().getPath();
      if (!StringUtils.endsWithIgnoreCase(path, SwaggerProvider.API_URI)) {
        return chain.filter(exchange);
      }
      ServerHttpRequest newRequest = request.mutate().build();
      ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
      return chain.filter(newExchange);
    };
  }
}